<!--
 * @Description: 串口调试页面
* 获取串口列表->选com1->创建链接,然后使用友善串口调试助手连接com2
 * @Date: 2024-02-02 11:51:30
 * @FilePath: \demod:\12electron\electron-demo\src\renderer\src\views\serialport\index.vue
-->
<template>
  <div class="container">
    <h3>串口调试页面</h3>
    <h2>
      开灯测试：
      <span v-if="state.result == 'FE 04 00 02 04 FD'">打开灯光</span>
      <span v-if="state.result == 'FE 04 00 03 05 FD'">灯光已关闭</span>
    </h2>

    <el-button type="primary" @click="getSerialPortList">获取串口列表</el-button>
    <br />
    <br />

    <el-form label-position="right" label-width="120px" :model="state" style="max-width: 900px">
      <el-form-item label="串口名称">
        <el-select v-model="state.serialName" placeholder="请选择串口名称" clearable>
          <el-option v-for="item in state.list" :key="item.path" :label="item.friendlyName" :value="item.path" />
        </el-select>
      </el-form-item>
      <el-form-item label="发送数据">
        <el-input v-model="state.transData" placeholder="发送数据" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="handleCreatConnection">创建连接</el-button>
        <el-button type="primary" @click="handleSendData">发送数据</el-button>
      </el-form-item>

      <el-form-item label="解析数据">
        <el-switch v-model="isUint8Array" />
      </el-form-item>

      <el-form-item label="接收数据">
        <textarea class="result-box" spellcheck="false" v-model="state.result"></textarea>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup name="serialport">
import { onMounted, reactive, ref } from 'vue';
import { transUint8ArrayToString } from '../../utils/transData.ts';
const isUint8Array = ref(false);
const state: any = reactive({
  list: [], // 串口列表
  serialName: '', // 串口名称
  transData: '', // 串口发送的数据

  result: '', // 主进程返回的数据
});

onMounted(async () => {
  // 监听主进程创建连接时返回的数据，并渲染到返回结果框
  window.electron.ipcRenderer.on('serialport:creatSerialPortConnection:reply', async (_event, data) => {
    state.result = data; // 返回结果框
  });

  // 监听主进程发送数据时返回的数据，并渲染到返回结果框
  window.electron.ipcRenderer.on('serialport:sendData:reply', async (_event, data) => {
    if (isUint8Array.value) {
      state.result = transUint8ArrayToString(data);
    } else {
      state.result = data; // 返回结果框
    }
  });
});

// 获取串口列表
const getSerialPortList = async () => {
  window.electron.ipcRenderer.send('serialport:getSerialPortList');
  // 监听主进程返回的数据，并渲染到返回结果框
  window.electron.ipcRenderer.on('serialport:getSerialPortList:reply', (_event, data) => {
    state.list = data; // 下拉列表
    state.result = JSON.stringify(data, null, 2); // 返回结果框
  });
};

// 创建端口链接
const handleCreatConnection = async () => {
  window.electron.ipcRenderer.send('serialport:creatSerialPortConnection', state.serialName);
};

// 发送串口数据
const handleSendData = async () => {
  window.electron.ipcRenderer.send('serialport:sendData', {
    path: state.serialName,
    data: state.transData,
  });
};
</script>

<style lang="scss" scoped>
.container {
  height: 90%;
  padding: 10px;
  font-size: 20px;
  color: #fff;
  overflow-y: scroll;

  .result-box {
    background-color: #fff;
    color: #333;
    width: 100%;
    height: 400px;
    overflow-y: scroll;
  }
}
</style>
